/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.third.openapi.enums;

import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @author duanlikao
 * @created 2021/1/4
 */
@Getter
@AllArgsConstructor
@NoArgsConstructor
public enum ErrorCodeEnum {

    ERROR_CODE_1001(1001, "服务执行成功"),
    ERROR_CODE_1002(1002, "参数不正确（参数为空、检索时间范围不正确、参数数量不正确）"),
    ERROR_CODE_1003(1003, "服务调用数量已达上限"),
    ERROR_CODE_1004(1004, "服务调用次数已达上限"),
    ERROR_CODE_1005(1005, "该账号未授权指定所属行政区划范围"),
    ERROR_CODE_1006(1006, "未查询到车辆轨迹"),
    ERROR_CODE_1010(1010, "用户名或密码不正确"),
    ERROR_CODE_1011(1011, "IP不在白名单列表"),
    ERROR_CODE_1012(1012, "账号已禁用"),
    ERROR_CODE_1013(1013, "账号已过有效期"),
    ERROR_CODE_1014(1014, "无服务权限"),
    ERROR_CODE_1015(1015, "用户认证系统已升级，请使用令牌访问"),
    ERROR_CODE_1016(1016, "令牌失效"),
    ERROR_CODE_1017(1017, "账号欠费"),
    ERROR_CODE_1018(1018, "授权的服务已禁用"),
    ERROR_CODE_1019(1019, "授权的服务已过期"),
    ERROR_CODE_1020(1020, "该车调用次数已达上限"),
    ERROR_CODE_1021(1021, "client_id 不正确"),
    ERROR_CODE_1031(1031, "签名验证失败"),
    ERROR_CODE_1050(1050, "没有购买此类套餐"),
    ERROR_CODE_1051(1051, "套餐服务能力已用尽"),
    ERROR_CODE_1052(1052, "用户不存在"),
    ERROR_CODE_1053(1053, "服务无对应有效套餐"),
    ERROR_CODE_9001(9001, "系统异常"),
    ;

    private  Integer code;
    private  String msg;

    private static Map <Integer, ErrorCodeEnum> codeMap = Maps.newHashMap();


    public Integer getCode() {
        return this.code;
    }

    public String getDesc() {
        return this.msg;
    }

    public static String getMsg(Integer code) {
        return codeMap.containsKey(code) ? codeMap.get(code).getDesc() : StringUtils.EMPTY;
    }

    public static boolean isValid(Integer code) {
        return codeMap.containsKey(code);
    }

    static {
        for (ErrorCodeEnum status : ErrorCodeEnum
                .values()) {
            codeMap.put(status.getCode(), status);
        }
    }

    public static ErrorCodeEnum getErrorCodeEnumByVal(Integer code) {
        return Arrays.stream(ErrorCodeEnum.values())
                .collect(Collectors.toMap(
                        ErrorCodeEnum::getCode, value -> value, (k1, k2) -> k2)).get(code);
    }
}
